<?php
/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/8/20
 * Time: 17:38
 * editor : zhouzhou
 */
namespace app\shop\api;

use app\common\api\BaseApi;
use app\shop\model\ShoppingCart;
use app\shop\api\ProductSkuApi;
use app\shop\api\ProductAttrApi;
use app\shop\api\OrdersApi;

class ShoppingCartApi extends  BaseApi{

    const ADD              = "shop/ShoppingCartApi/add";
    const COUNT            = "shop/ShoppingCartApi/count";
    const DELETE           = "shop/ShoppingCartApi/delete";
    const GET_INFO         = "shop/ShoppingCartApi/getInfo";
    const QUERY            = "shop/ShoppingCartApi/query";
    const QUERY_NO_PAGING  = "shop/ShoppingCartApi/queryNoPaging";
    const QUERY_WITH_STORE = "shop/ShoppingCartApi/queryWithStore";
    const GET_WITH_STORE   = "shop/ShoppingCartApi/getWithStore";
    const SAVE_BY_ID       = "shop/ShoppingCartApi/saveById";
    const CHECK_ID         = "shop/ShoppingCartApi/checkId";
    const SAVE             = "shop/ShoppingCartApi/save";

    protected  function _init(){
        $this -> model = new ShoppingCart();
    }

    //检查购物车限购和库存
    //flus true:修改当前购物车个数 false:替换当前购物车个数
    public function checkID($id,$count=0,$flus=false){
        //购物车信息
        $result = $this->getInfo(['id'=>$id]);
        if(!$result['status']) return $this->apiReturnErr($result['info']);
        if(empty($result['info']))  return $this->apiReturnErr('购物车失效:'.$id);
        $psku_id    = $result['info']['psku_id'];
        $uid        = $result['info']['uid'];
        $gid        = $result['info']['group_id'];

        return apiCall("shop/ProductSkuApi/checkSkuID",[$uid,$psku_id,$count,$flus,$gid]);
    }

    public function delete($map){
        if(isset($map['id']) && is_array($map['id'])){
            $p = $map['id'][1];unset($map['id']);
            $map['id'] = ['in',$p];
        }
        $result = $this -> model -> where($map) -> delete();
        if (false === $result) return $this -> apiReturnErr($this -> model -> getDbError());
        return $this -> apiReturnSuc($result);
    }
    /**
     * 查询左链接包含店铺信息
     * @param $map
     * @return array
     */
    public function queryWithStore($map,$find = false){
        $result = $this->model->alias("cart")
        ->field('p.name,p.dt_goods_unit,p.dt_origin_country,store.uid as store_creator,cart.id,cart.uid,cart.store_id,cart.p_id,cart.sku_id,cart.sku_desc,cart.icon_url,cart.package_id,cart.count,cart.name,cart.express,cart.price,cart.group_id,cart.ori_price,cart.create_time,cart.psku_id,cart.weight,cart.tax_rate,store.service_phone,store.weixin_number,store.logo as store_logo,store.longitude,store.latitude,store.exp as store_exp')
        ->join("LEFT JOIN __STORE__ as store on store.id = cart.store_id")
        ->join("LEFT JOIN __PRODUCT__ as p on p.id = cart.p_id")
        ->where($map);//->fetchSql(true)
        if($find) $result=$result->find();
        else $result = $result->select();

        // $this->apiReturnErr($result);
        if(false === $result){
            return $this->apiReturnErr($this->model->getDbError());
        }else{
            return $this->apiReturnSuc($result);
        }

    }

    public function getWithStore($map){
        return $this->queryWithStore($map,true);
    }
}